<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="系统订单号" prop="platOrderNo">
        <el-input
          v-model="queryParams.platOrderNo"
          placeholder="请输入系统订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="售后单号" prop="platAfterNo">
        <el-input
          v-model="queryParams.platAfterNo"
          placeholder="请输入售后单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="售后类型" prop="afterType">
        <el-select v-model="queryParams.afterType" placeholder="请选择售后类型" clearable>
          <el-option
            v-for="dict in dict.type.after_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="售后状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择售后状态" clearable>
          <el-option
            v-for="dict in dict.type.after_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="运单号" prop="nu">
        <el-input
          v-model="queryParams.nu"
          placeholder="请输入运单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:orderAfter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:orderAfter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:orderAfter:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orderAfter:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderAfterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
  <!--    <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="系统订单号" align="center" prop="platOrderNo" />
      <el-table-column label="订单商品id" align="center" prop="orderGoodsId" />
      <el-table-column label="售后单号" align="center" prop="platAfterNo" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="售后类型" align="center" prop="afterType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.after_type" :value="scope.row.afterType"/>
        </template>
      </el-table-column>
      <el-table-column label="售后数量" align="center" prop="afterNum" />
      <el-table-column label="售后原因" align="center" prop="explain" />
      <el-table-column label="售后状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.after_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="refundMoney" />
      <el-table-column label="追缴订单编号" align="center" prop="recoveryOrderNo" />
      <el-table-column label="运单照片" align="center" prop="idcardReverse" >
        <template slot-scope="scope">
          <el-image

            style="width: 50px; height: 50px; margin-bottom:-4px"
            :src="`${ scope.row.logisticsUrl}`"
            :preview-src-list="[scope.row.logisticsUrl]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="快递公司编号" align="center" prop="com" />
      <el-table-column label="运单号" align="center" prop="nu" />
      <el-table-column label="退款状态" align="center" prop="refundStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.refundStatus == 0">无</span>
          <span v-if="scope.row.refundStatus == 1">已退款</span>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="opinion" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="text"
            @click="receive(scope.row)"
          >通过</el-button>
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="text"
           style="color: red"
            @click="showReject(scope.row)"

          >驳回</el-button>
          <el-button
            v-if="scope.row.status == 2||scope.row.status == 20"
            size="mini"
            type="text"
            @click="showFinish(scope.row)"
          >完成售后</el-button>
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:orderAfter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:orderAfter:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="系统订单号" prop="platOrderNo">
          <el-input v-model="form.platOrderNo" placeholder="请输入系统订单号" />
        </el-form-item>
        <el-form-item label="订单商品id" prop="orderGoodsId">
          <el-input v-model="form.orderGoodsId" placeholder="请输入订单商品id" />
        </el-form-item>
        <el-form-item label="售后单号" prop="platAfterNo">
          <el-input v-model="form.platAfterNo" placeholder="请输入售后单号" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="售后类型" prop="afterType">
          <el-select v-model="form.afterType" placeholder="请选择售后类型">
            <el-option
              v-for="dict in dict.type.after_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="售后数量" prop="afterNum">
          <el-input v-model="form.afterNum" placeholder="请输入售后数量" />
        </el-form-item>
        <el-form-item label="售后原因" prop="explain">
          <el-input v-model="form.explain" placeholder="请输入售后原因" />
        </el-form-item>
        <el-form-item label="售后状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.after_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退款金额" prop="refundMoney">
          <el-input v-model="form.refundMoney" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="追缴订单编号" prop="recoveryOrderNo">
          <el-input v-model="form.recoveryOrderNo" placeholder="请输入追缴订单编号" />
        </el-form-item>
        <el-form-item label="快递公司编号" prop="com">
          <el-input v-model="form.com" placeholder="请输入快递公司编号" />
        </el-form-item>
        <el-form-item label="运单号" prop="nu">
          <el-input v-model="form.nu" placeholder="请输入运单号" />
        </el-form-item>
        <el-form-item label="审核意见" prop="opinion">
          <el-input v-model="form.opinion" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--驳回-->
    <el-dialog title="填写驳回说明" :visible.sync="rejectFlag" width="500px" append-to-body>
      <el-form ref="form2" :model="form2"  label-width="80px">
        <el-form-item label="驳回说明" prop="opinion" :rules="[{ required: true, message: '请输入驳回说明', trigger: 'blur' }
                                                   ]">
          <el-input v-model="form2.opinion" placeholder="请输入驳回说明"  />
        </el-form-item>
      </el-form>
     <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="reject">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!--完成-->
    <el-dialog title="完成售后订单" :visible.sync="finishFlag" width="500px" append-to-body>
      <el-form ref="form3" :model="form3"  label-width="160px">
        <el-form-item label="用户需补偿金额" prop="afterphotos" :rules="[{ required: true, message: '请输入用户需补偿金额', trigger: 'blur' }]">
          <!-- <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form3.afterphotos" :src="form3.afterphotos" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload> -->
            <ImageUpload limit="1" v-model="form3.afterphotos" ></ImageUpload>
        </el-form-item>
        <el-form-item label="用户需补偿金额" prop="compensationAmount" :rules="[{ required: true, message: '请输入用户需补偿金额', trigger: 'blur' }
                                                   ]">
          <el-input   oninput="value=value.replace(/[^\d.]/g, '').replace(/(\..*?)\..*/g, '$1').replace(/^0[^.]/, '0')"  v-model="form3.compensationAmount" placeholder="请输入用户需补偿金额"  />
        </el-form-item>
        <el-form-item label="补偿原因" prop="content" :rules="[{ required: true, message: '请输入补偿原因', trigger: 'blur' }
                                                   ]">
          <el-input type="textarea"
          :rows="2"  v-model="form3.content" placeholder="请输入补偿原因"  />

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="finish">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listOrderAfter, getOrderAfter, delOrderAfter,
  addOrderAfter, updateOrderAfter,updateOrderAfterStatus } from "@/api/system/orderAfter";

export default {
  name: "OrderAfter",
  dicts: ['after_type', 'after_status'],
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      form2:{
        opinion:'',
      },
      form3:{
        compensationAmount:0,
        content:"",
        afterphotos:"",
      },
      selItem:{},
      rejectFlag : false,
      finishFlag : false,


      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后订单表格数据
      orderAfterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createdAt: null,
        updatedAt: null,
        platOrderNo: null,
        orderGoodsId: null,
        platAfterNo: null,
        userId: null,
        afterType: null,
        afterNum: null,
        explain: null,
        status: null,
        refundMoney: null,
        recoveryOrderNo: null,
        com: null,
        nu: null,
        refundStatus: null,
        opinion: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  mounted(){
console.log(uploadUrl,"sdi");

  },
  methods: {
    //通过
    receive(item){
      let that = this
      this.$modal.confirm('是否通过该售后订单申请？').then(function() {
       let data ={
         platAfterNo:item.platAfterNo,
         status:2,
       }
        return that.updateStatus(data);
      })
    },
    //驳回
    showReject(item){
      this.form2.opinion = ''
      this.selItem = item
      this.rejectFlag = true
      console.log(this.selItem)
      console.log(item)
    },
    reject(){
      let item = this.selItem
      if(!this.form2.opinion){
        return this.$modal.msgWarning("请填写驳回说明");
      }
      let data ={
        platAfterNo:item.platAfterNo,
        status:5,
        opinion:this.form2.opinion,
      }
      this.updateStatus(data);
    },
    showFinish(item){
      this.form3.compensationAmount = 0
      this.selItem = item
      this.finishFlag = true
    },
    //完成售后
    finish(){
      let item = this.selItem
      let data ={
        platAfterNo:item.platAfterNo,
        status:3,
        compensationAmount:this.form3.compensationAmount,
        afterPicture:this.form3.afterphotos,
        afterType:4,
        ...this.form3
      }
      console.log(this.form3,this.selItem);

      this.updateStatus(data);
    },


    //修改状态
    updateStatus(data){
      updateOrderAfterStatus(data).then(res=>{
        this.$modal.msgSuccess("操作成功");
        this.cancel();
        this.getList();
      })
    },
    /** 查询售后订单列表 */
    getList() {
      this.loading = true;
      listOrderAfter(this.queryParams).then(response => {
        this.orderAfterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.rejectFlag = false;
      this.finishFlag = false;
      this.form3.compensationAmount = 0;
      this.form2.opinion = '';
      this.selItem ={}
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createdAt: null,
        updatedAt: null,
        platOrderNo: null,
        orderGoodsId: null,
        platAfterNo: null,
        userId: null,
        afterType: null,
        afterNum: null,
        explain: null,
        status: null,
        inProgress: null,
        refundMoney: null,
        recoveryOrderNo: null,
        com: null,
        nu: null,
        refundStatus: null,
        opinion: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderAfter(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改售后订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrderAfter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderAfter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除售后订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrderAfter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orderAfter/export', {
        ...this.queryParams
      }, `orderAfter_${new Date().getTime()}.xlsx`)
    },
    handleAvatarSuccess(res, file) {
      console.log(URL.createObjectURL(file.raw));
      console.log(file);

        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        console.log(file,"faojfo");

        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return  isLt2M;
      }
  }
};
</script>
